﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.SqlConnector;
using Vintner.Database;
using Vintner.Library;

namespace Vintner.Forms
{
	public partial class Description : ApplicationLibrary.DBManager.DocumentForm
	{
		private string Category;

		public Description()
		{
			InitializeComponent();
		}

		public Description(GlobalInfo _gInfo, string Category)
            :base(_gInfo, "Description")
        {
			this.Category = Category;
            InitializeComponent();
        }

		#region Virtual Method
		public override string DefineBrowseQuery()
		{
			QueryBuilder qb = new QueryBuilder();

			qb.Clear();
			qb.AddSelectAll(AM_Descriptions.Name);
            qb.AddFrom(AM_Descriptions.Name);
            qb.AddCompare(AM_Descriptions.Category, string.Concat("'", Category, "'"));
            qb.AddOrder(AM_Descriptions.Description);

			return qb.Query;
		}

		public override string GetCodeColumn()
		{
            return AM_Descriptions.ID.Name;
		}

		public override Table GetRadarTable()
		{
            return new AM_Descriptions();
		}

		protected override void OnAttachData()
		{
			dbManager = new dbmanagerDescription("descriptionForm", base.providerType, ConnectionString, Category);
            dbManager.AddMaster(this, AM_Descriptions.Name, true);
		}

		protected override void OnBindData()
		{
            BindControl(txtDescription, AM_Descriptions.Description);
		}


		protected override void FocusOnNew()
		{
			txtDescription.Focus();
		}

		#endregion

	}

	public class PaymentForm : Description
	{
		public PaymentForm(GlobalInfo _gInfo)
			: base(_gInfo, "Payment")
		{
			this.Text = global::Vintner.Properties.Resources.F_Payment;
		}
	}

	#region DbManager dbmanagerDescription
	/// <summary>
	/// Classe derivata per gestioni particolari
	/// </summary>
	internal class dbmanagerDescription : DBManager
	{
		private string Category;

		public dbmanagerDescription(string name, ProviderType providerType, string conString, string Category)
			: base(name, providerType, conString)
		{
			this.Category = Category;
		}

		protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
		{
			if (e.StatementType == StatementType.Insert)
			{
                e.Row[AM_Descriptions.Category.Name] = Category;
			}
			base.dAdapter_MasterRowUpdating(sender, e);
		}

		protected override string CreateMasterQuery()
		{
            this.Table = new AM_Descriptions();
			QueryBuilder qB = new QueryBuilder();
			qB.AddSelect();
			qB.AddFrom(this.Table);
			qB.AddCompare(this.Table.ForeignKey, "@p1");
            qB.AddCompare(AM_Descriptions.Category, string.Concat("'", Category, "'"));

			return qB.Query;
		}

        protected override List<SqlABParameter> CreateMasterParam()
		{
            List<SqlABParameter> PList = new List<SqlABParameter>();

			SqlABParameter sParam = new SqlABParameter(providerType, "@p1", SqlDbType.Int);
			sParam.Value = 0;
            PList.Add(sParam);
            return PList;
		}
	}
	#endregion
}
